package br.com.obardavez.util.impl;

import java.io.Serializable;
import java.text.Normalizer;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.WordUtils;

import br.com.obardavez.util.StringUtil;
import br.com.obardavez.util.Util;
import br.com.obardavez.util.enums.SideEnum;

import com.google.inject.Inject;

public class StringUtilImpl implements Serializable, StringUtil {

	private static final long serialVersionUID = 1L;

	@Inject
	private Util util;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#contains(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public Boolean contains(String value, String... valueList) {

		Boolean contains = false;
		if (value != null && !value.equals("") && valueList != null
				&& valueList.length > 0) {
			for (String valueItem : valueList) {
				if (valueItem.equals(value)) {
					contains = true;
					break;
				}
			}
		}

		return contains;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#isEmpty(java.lang.String)
	 */
	@Override
	public Boolean isEmpty(String value) {
		return value == null || value.trim().equals("");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.StringUtil#isNotEmpty(java.lang.String)
	 */
	@Override
	public Boolean isNotEmpty(String value) {
		return !isEmpty(value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.StringUtil#uncaptalize(java.lang.String)
	 */
	@Override
	public String uncaptalize(String value) {
		return value.substring(0, 1).toLowerCase() + value.substring(1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.StringUtil#captalize(java.lang.String)
	 */
	@Override
	public String captalize(String value) {
		return value.substring(0, 1).toUpperCase() + value.substring(1);
	}
	
	/**
	 * Retorna o mesmo texto informado com a primeira letra de cada palavra em
	 * maiusculo
	 * 
	 * @param value
	 *            Texto a ser alterado
	 * @return Texto com a primeira letra de cada palavra em maiusculo
	 */
	@Override
	public String capitalizeFully(String value) {
		return WordUtils.capitalizeFully(value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#fill(java.lang.String,
	 * java.lang.Integer, java.lang.Character)
	 */
	@Override
	public String fill(String text, Integer length, Character fillCharacter) {
		return fill(text, length, fillCharacter, SideEnum.LEFT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#fill(java.lang.Number,
	 * java.lang.Integer, java.lang.Character)
	 */
	@Override
	public String fill(Number number, Integer length, Character fillCharacter) {
		return fill(number != null ? number.toString() : null, length,
				fillCharacter, SideEnum.LEFT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#fill(java.lang.Number,
	 * java.lang.Integer, java.lang.Character,
	 * com.digicade.android.util.enums.SideEnum)
	 */
	@Override
	public String fill(Number number, Integer length, Character fillCharacter,
			SideEnum fillSide) {
		return fill(number != null ? number.toString() : null, length,
				fillCharacter, fillSide);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#fill(java.lang.String,
	 * java.lang.Integer, java.lang.Character,
	 * com.digicade.android.util.enums.SideEnum)
	 */
	@Override
	public String fill(String text, Integer length, Character fillCharacter,
			SideEnum fillSide) {

		if (isNotEmpty(text) && text.length() < length && fillCharacter != null
				&& fillSide != null) {

			while (text.length() < length) {
				if (fillSide.equals(SideEnum.LEFT)) {
					text = fillCharacter + text;
				} else if (fillSide.equals(SideEnum.RIGHT)) {
					text += fillCharacter;
				} else {
					throw new RuntimeException(
							"Error filling text: Invalid fill side.");
				}
			}
		}

		return text;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.StringUtil#includeSingleQuotes(java.lang
	 * .String)
	 */
	@Override
	public String includeSingleQuotes(String text) {
		return includeSingleQuotes(text, DEFAULT_DELIMITER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.digicade.android.util.impl.StringUtil#includeSingleQuotes(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public String includeSingleQuotes(String text, String delimiter) {
		String resultText = "";
		if (isNotEmpty(text)) {
			String[] tokens = text.split(delimiter);
			for (String token : tokens) {
				resultText += resultText.equals("") ? "" : delimiter;
				resultText += "'" + token + "'";
			}
		}
		return resultText;
	}
	
	@Override
	public String escapeJavascript(String text) {
		return StringEscapeUtils.escapeJavaScript(text);
	}
	public String escapeHtml(String text) {
		return StringEscapeUtils.escapeHtml(text);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#insert(java.lang.String,
	 * int, java.lang.String)
	 */
	@Override
	public String insert(String originalText, int index, String textToInclude) {
		if (isNotEmpty(originalText)) {

			if (index < 0) {
				return textToInclude + originalText;
			} else if (index >= originalText.length()) {
				return originalText + textToInclude;
			} else {
				return originalText.substring(0, index) + textToInclude
						+ originalText.substring(index);
			}
		}
		return originalText;
	}
	
	public String createIdForBox(String nameBox){
		nameBox = nameBox.toLowerCase();
		
		nameBox = Normalizer.normalize(nameBox, Normalizer.Form.NFD);
		nameBox = nameBox.replaceAll("[^\\p{ASCII}]", "");
		
		return nameBox.replaceAll(" ", "_");
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.StringUtil#getUtil()
	 */
	@Override
	public Util getUtil() {
		return util;
	}
	
	public static void main(String[] args) {
		
	

	
		
	}
	
}
